package it.unina.lab.parsing;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import diuf.diva.hephaistk.xml.HephaisTKParsingException;
import diuf.diva.hephaistk.xml.MalformedXMLException;
import diuf.diva.hephaistk.xml.XMLDocumentReader;
import diuf.diva.hephaistk.xml.smuiml.Action;
import diuf.diva.hephaistk.xml.smuiml.Actions;
import diuf.diva.hephaistk.xml.smuiml.Context;
import diuf.diva.hephaistk.xml.smuiml.Recognizer;
import diuf.diva.hephaistk.xml.smuiml.Recognizers;
import diuf.diva.hephaistk.xml.smuiml.SMUIML;
import diuf.diva.hephaistk.xml.smuiml.Trigger;
import diuf.diva.hephaistk.xml.smuiml.Triggers;

public class ParserSmuiml {

	private String smuimlFileToParse;
	private Logger logger = Logger.getLogger(ParserSmuiml.class);
	private ArrayList<Recognizer> recognizers;
	private ArrayList<Trigger> triggers;
	private ArrayList<Action> actions;
	private ArrayList<Context> contexts;
	private SMUIML smuimlFileParsed;
	
	public ParserSmuiml(String file){
		this.smuimlFileToParse = file;	
		this.smuimlFileParsed = null;
		this.contexts = new ArrayList<Context>();
		this.recognizers = new ArrayList<Recognizer>();
		this.triggers = new ArrayList<Trigger>();
		this.actions = new ArrayList<Action>();
	}

	
	
	public SMUIML parse_file(){
		XMLDocumentReader smuiml_document = null;
		try {
			smuiml_document = new XMLDocumentReader(smuimlFileToParse);
		} catch (FileNotFoundException e) {
			logger.error("couldn't locate SMUIML file");
			logger.error(e);
		} catch (MalformedXMLException e) {
			logger.error("XML error in SMUIML file");
			logger.error(e);
		} catch (IOException e) {
			logger.error(e);
		} catch (HephaisTKParsingException e) {
			logger.error(e);
		}
		
		smuimlFileParsed = (SMUIML) smuiml_document.getXMLDocument();
		parsingActionsSection();
		parsingRecognizersSection();
		parsingTriggersSection();
		parsingContextSection();
		logger.info("Parsing complete...");
		//logger.info(smuimlFileParsed);
		return smuimlFileParsed;
	}
	
	
	
	
	
	private void parsingRecognizersSection(){
		if(smuimlFileParsed != null)
			recognizers.addAll(smuimlFileParsed.getIntegrationDesc(0).getRecognizers().getRecognizers());
		else
			logger.error("SMUILFIle ancora non parserizzato!");
		
	}
	
	
	private void parsingTriggersSection(){
		if(smuimlFileParsed != null)
			triggers.addAll(smuimlFileParsed.getIntegrationDesc(0).getTriggers().getTriggers());
		else
			logger.error("SMUILFIle ancora non parserizzato!");
	}
	
	
	private void parsingActionsSection(){
		if(smuimlFileParsed != null)
			actions.addAll(smuimlFileParsed.getIntegrationDesc(0).getActions().getActions());
		else
			logger.error("SMUILFIle ancora non parserizzato!");
	}
	
	
	private void parsingContextSection(){
		if(smuimlFileParsed != null)
			contexts.addAll(smuimlFileParsed.getIntegrationDesc(0).getDialog().getContexts());
		else
			logger.error("SMUILFIle ancora non parserizzato!");
	}



	public String getSmuimlFileToParse() {
		return smuimlFileToParse;
	}



	public void setSmuimlFileToParse(String smuimlFileToParse) {
		this.smuimlFileToParse = smuimlFileToParse;
	}



	public ArrayList<Recognizer> getRecognizers() {
		return recognizers;
	}



	public void setRecognizers(ArrayList<Recognizer> recognizers) {
		this.recognizers = recognizers;
	}



	public ArrayList<Trigger> getTriggers() {
		return triggers;
	}



	public void setTriggers(ArrayList<Trigger> triggers) {
		this.triggers = triggers;
	}



	public ArrayList<Action> getActions() {
		return actions;
	}



	public void setActions(ArrayList<Action> actions) {
		this.actions = actions;
	}



	public ArrayList<Context> getContexts() {
		return contexts;
	}



	public void setContexts(ArrayList<Context> contexts) {
		this.contexts = contexts;
	}


	
	
	
	
	
	
	
	
	
}
